package com.itheima.service.impl;

/**
 * @author Dugen.Zhu
 * @date 2019-12-29 11:11
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckItemService;
import com.itheima.dao.CheckItemDao;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    //新增检查项
    @Override
    public Result add(CheckItem checkItem) {
        //参数校验，输入的项目编码或项目名称必须唯一，不能重复
        CheckItem checkItem1 = checkItemDao.findByCodeOrName(checkItem.getCode(), checkItem.getName());
        if (checkItem1 != null) {
            return new Result(false, "您输入的项目编码或项目名称已存在");
        } else {
            checkItemDao.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        /*传过来的queryPageBean的参数
        private Integer currentPage;//页码
         private Integer pageSize;//每页记录数
         private String queryString;//查询条件*/
        /*返回的PageResult有总记录数和结果集合
        private Long total;//总记录数
         private List rows;//当前页结果*/
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> page;
        String queryString = queryPageBean.getQueryString();
        if (queryString == null || queryString.length() == 0 || queryString.equals("") || queryString.trim().isEmpty()) {
            page = checkItemDao.findPage();
        } else {
            page = checkItemDao.findByCondition(queryString);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Integer id) {
        checkItemDao.delete(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public List<Integer> findItemIdsByGroupId(Integer id) {
        return checkItemDao.findItemIdsByGroupId(id);
    }

}
